#!/usr/bin/env bash
# Copyright (c) 2023 The Toltec Contributors
# SPDX-License-Identifier: MIT

archs=(rmall rmallos2 rmallos3 rm1 rm1os2 rm1os3 rm2 rm2os2 rm2os3)
pkgnames=(toltec-base)
pkgdesc="Metapackage defining the base set of packages in a Toltec install"
url=https://toltec-dev.org/
pkgver=1.4-2
timestamp=2023-12-27T08:30Z
section="utils"
maintainer="Eeems <eeems@eeems.email>"
license=MIT
installdepends=(toltec-bootstrap toltec-deletions toltec-completion launcherctl wget-ssl ca-certificates entware-rc)
installdepends_rm1os2=(open-remarkable-shutdown)
installdepends_rm1os3=(open-remarkable-shutdown)
installdepends_rm2os2=(rm2-suspend-fix)
installdepends_rm2os3=(rm2-suspend-fix)

source=(opt-usr-bin.sh)
sha256sums=(SKIP)

package() {
    install -D -m 755 -t "$pkgdir"/etc/profile.d/ "$srcdir"/opt-usr-bin.sh
}

configure() {
    ln -sf /opt/etc/profile /etc/profile.d/toltec.sh
    # shellcheck disable=SC2016
    sed -i \
        -e 's|^export TERM=xterm|if \[ -z "$TERM" \];then export TERM=xterm;fi|' \
        /opt/etc/profile
    echo "Disabling automatic update"
    disable-unit update-engine.service
    case "$arch" in
        rmall | rmallos2 | rmallos3)
            echo "Warning: rmall version of toltec-base should not be installed"
            echo "Please run opkg install --force-reinstall toltec-base"
            ;;
        rm1 | rm1os2 | rm1os3)
            if ! is-masked sys-subsystem-net-devices-usb1.device; then
                systemctl mask sys-subsystem-net-devices-usb1.device
            fi
            if ! is-masked busybox-ifplugd@usb1.service; then
                systemctl mask busybox-ifplugd@usb1.service
            fi
            ;;
        rm2 | rm2os2 | rm2os3)
            if is-masked sys-subsystem-net-devices-usb1.device; then
                systemctl unmask sys-subsystem-net-devices-usb1.device
            fi
            if is-masked busybox-ifplugd@usb1.service; then
                systemctl unmask busybox-ifplugd@usb1.service
            fi
            ;;
    esac
}

postremove() {
    if is-masked sys-subsystem-net-devices-usb1.device; then
        systemctl unmask sys-subsystem-net-devices-usb1.device
    fi
    if is-masked busybox-ifplugd@usb1.service; then
        systemctl unmask busybox-ifplugd@usb1.service
    fi
    if ! is-enabled update-engine.service; then
        systemctl enable update-engine
    fi
}
